﻿using Exam.BLL;
using Exam.DBHelper;
using Exam.Model;
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class MeetRoomController : Controller
    {
        //
        // GET: /MeetRoom/
        ProvinceService provinceService = new ProvinceService();
        MeetRoomService meetroomService = new MeetRoomService();
        CityService cityService = new CityService();
        public ActionResult RoomList()
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);
            return View();
        }

        public ActionResult RoomAdd()
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);
            return View();
        }

        public ActionResult RoomUpdate(string id)
        {
            ViewData["ProvinceList"] = provinceService.LoadEntites(e => e.IsActive == 0);

            MeetRoom meetroom = meetroomService.LoadEntites(e => e.RoomID == Guid.Parse(id)).FirstOrDefault();
            ViewData["MeetRoom"] = meetroom;
            if (meetroom != null)
            {
                ViewData["_CityList"] = cityService.LoadEntites(e => e.ProvinceID == meetroom.ProvinceID);
            }
            else
            {
                ViewData["_CityList"] = null;
            }
            return View();
        }

        #region 页面事件
         [AllowAnonymous]
        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 1 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }
            int totalcount = 0;
            string provinceID = Request["provinceid"] == null ? "" : Request["provinceid"];
            string cityID = Request["cityid"] == null ? "" : Request["cityid"];
            string RoomName = Request["roomname"] == null ? "" : Request["roomname"];

            string IsActive = Request["isactive"] == null ? "-1" : Request["isactive"];
            List<Exam.Model.ExtentionModels.ExMeetRoom> listRoom = new Exam.BLL.ExtendBLL.MeetRoom().GetRoomList(provinceID, cityID, RoomName, IsActive,DataProp,SortDir=="asc", pageStart, pageSize, out totalcount);
            var result = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = listRoom, sEcho = 0 };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
         [AllowAnonymous]
        public string AddInfo(MeetRoom meetroom)
        {
            try
            {
                if (IsExistData(meetroom).RoomID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    meetroom.RoomID = Guid.NewGuid();
                    DateTime time = DateTime.Now;
                    meetroom.CreateTime = time;
                    meetroom.CreateUserID = UserAccount.UserID;
                    meetroom.UpdateTime = time;
                    MeetRoom newRoom = meetroomService.AddEntity(meetroom);

                    return newRoom.RoomID.ToString();

                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "";
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public string UpdateInfo(MeetRoom meetroom)
        {
            try
            {
                MeetRoom _pro = IsExistData(meetroom);
                if (_pro.RoomID.ToString() == "00000000-0000-0000-0000-000000000000" || _pro.RoomID == meetroom.RoomID)
                {
                    meetroom.UpdateTime = DateTime.Now;
                    meetroom.UpdateUserID = UserAccount.UserID;
                    meetroomService.UpdateEntity(meetroom);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }
        #endregion

        #region 公用
        public MeetRoom IsExistData(MeetRoom meetroom)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@RoomName", System.Data.SqlDbType.NVarChar);
            parname.Value = meetroom.RoomName;
            dic.Add("RoomName", parname);

            SqlParameter parcityid = new SqlParameter("@CityID", System.Data.SqlDbType.UniqueIdentifier);
            parcityid.Value = meetroom.CityID;
            dic.Add("CityID", parcityid);

            return commonservice.getExistData<MeetRoom>("MeetRoom", dic);
        }

        #endregion
    }
}
